package com.zhangtingshu.controller;

import com.zhangtingshu.pojo.Comment;
import com.zhangtingshu.pojo.Invitation;
import com.zhangtingshu.pojo.User;
import com.zhangtingshu.service.CommentService;
import com.zhangtingshu.service.InvitationService;
import com.zhangtingshu.service.impl.InvitationServiceImpl;
import com.zhangtingshu.vo.InvitationVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author zhangtingshu
 * 帖子控制器
 */
@Controller
public class InvitationController {

    Logger logger = Logger.getLogger(InvitationController.class);

    @Autowired
    private InvitationServiceImpl invitationService;

    @Autowired
    private CommentService commentService;

    //进入首页
    @RequestMapping({"/index", "/"})
    public String index() {
        return "redirect:list.action";
    }

    //    模糊查询和查询全部
    @RequestMapping("/list")
    public ModelAndView list(String searchInfo) {
        System.out.println("searchInfo = " + searchInfo);
        List<InvitationVO> list = invitationService.findAll(searchInfo);
//        logger.info("list"+list);
        ModelAndView mv = new ModelAndView("list");
        mv.addObject("list", list);
        return mv;
    }

    @RequestMapping("/personlist")
    public String mylist(int uid, Model model) {
        List<InvitationVO> list = invitationService.findPersonList(uid);
        logger.info(list.size());
        model.addAttribute("list", list);
        return "list";
    }

    @RequestMapping("/add")
    public String add() {
        return "add";
    }

    @RequestMapping("/addsave")
    public String addsave(@ModelAttribute Invitation invitation, HttpSession session) {
        //从会话中得到当前登录的人，将当前人的id放到帖子对象当中
        User user = (User) session.getAttribute("currentUser");
        invitation.setUid(user.getUid());
        boolean flag = invitationService.insert(invitation);
        logger.info(flag ? "保存成功" : "保存失败");
        return "redirect:/list.action";
    }

    @RequestMapping("/delete")
    public String delete(int[] ids) {
        logger.info(Arrays.toString(ids));
        invitationService.delete(ids);
        return "redirect:/list.action";
    }

    @RequestMapping("/findbyid")
    public String findbyId(int id, Model model) {
        Invitation invitation = invitationService.findById(id);//通过模型得到了帖子对象
        model.addAttribute("invitation", invitation);
//        获取评论信息
        List<Comment> commentList = commentService.findCommentByIid(id);
        logger.info("commentList" + commentList);
        model.addAttribute("comment", commentList);

        return "info";
    }

    @RequestMapping("/update")
    public String update(int id, Model model) {
        logger.info(id);
        Invitation invitation = invitationService.findById(id);//通过模型得到了帖子对象
        model.addAttribute("invitation", invitation);
        return "update";
    }

    @RequestMapping("/updatesave")
    public String updatesave(@ModelAttribute Invitation invitation) {
        logger.info(invitation);
        boolean flag = invitationService.update(invitation);
        return "redirect:/findbyid.action?id=" + invitation.getId();
    }

    @RequestMapping("/addLike")
    @ResponseBody
    public Map<String, String> addLike(int id, int num) {
        logger.info("点赞 id:" + id + ",num:" + num);
        invitationService.addLike(id, num + 1);
        Map<String, String> map = new HashMap<>();
        map.put("true", "点赞成功");
        return map;
    }


}























